import configparser
import getpass

import common


def read():
    # 读取配置文件
    file = r"C:\\Users\\" + getpass.getuser() + r"\\AppData\\Roaming\\weixin_config.ini"
    #print(file)
    config = configparser.ConfigParser() 
    config.read(file, encoding='utf-8')
    return config

def getConfig():
    file = r"C:\\Users\\" + getpass.getuser() + r"\\AppData\\Roaming\\weixin_config.ini"
    # print(file)
    config = configparser.ConfigParser() 
    config.read(file, encoding='utf-8')

    # 读取内容
    txtContent = []
    i = 1
    while True:
        try:
            content = config.get('Content Set', 'Content' + str(i))
            txtContent.append(content)
            i += 1
        except configparser.NoOptionError:
            break
    
    # 读取通讯录模式
    contracts = {}
    contracts['contractsNumber']    = config.get('Contracts Set', 'contractsNumber')
    contracts['contractsTime']      = config.get('Contracts Set', 'contractsTime')
    contracts['timeInterval']       = config.get('Contracts Set', 'timeInterval')
    contracts['radioContent']       = config.get('Contracts Set', 'radioContent')
    contracts['radioImage']         = config.get('Contracts Set', 'radioImage')

    # 读取群转发模式
    group = {}
    group['groupKeyword']       = config.get('Group Set', 'groupKeyword')
    group['groupMemberNumber']  = config.get('Group Set', 'groupMemberNumber')
    group['groupNumber']        = config.get('Group Set', 'groupNumber')
    group['groupTime']          = config.get('Group Set', 'groupTime')
    group['timeGroupInterval']  = config.get('Group Set', 'timeGroupInterval')
    group['radioGroupContent']       = config.get('Group Set', 'radioGroupContent')
    group['radioGroupImage']         = config.get('Group Set', 'radioGroupImage')
    group['radioGroupLastMessage']   = config.get('Group Set', 'radioGroupLastMessage')

    # 读取好友转发模式
    friend = {}
    friend['friendKeyword']            = config.get('Friend Set', 'friendKeyword')
    friend['friendNumber']             = config.get('Friend Set', 'friendNumber')
    friend['friendTime']               = config.get('Friend Set', 'friendTime')
    friend['timeFriendInterval']       = config.get('Friend Set', 'timeFriendInterval')
    friend['radioFriendContent']       = config.get('Friend Set', 'radioFriendContent')
    friend['radioFriendImage']         = config.get('Friend Set', 'radioFriendImage')
    friend['radioFriendLastMessage']   = config.get('Friend Set', 'radioFriendLastMessage')

    
    # 读取朋友圈设置
    moment = {}
    moment['checkboxMoment']            = config.get('Moment Set', 'checkboxMoment')
    moment['momentNumber']             = config.get('Moment Set', 'momentNumber')
    moment['timeMomentInterval']       = config.get('Moment Set', 'timeMomentInterval')
    moment['radioMomentContent']       = config.get('Moment Set', 'radioMomentContent')
    moment['radioMomentImage']         = config.get('Moment Set', 'radioMomentImage')
    moment['radioMomentLastMessage']   = config.get('Moment Set', 'radioMomentLastMessage')

    data = {}
    data['content'] = txtContent
    data['contracts'] = contracts
    data['group'] = group
    data['friend'] = friend
    data['moment'] = moment
    # 返回给js, mac地址
    data['mac'] = common.get_mac()
    
    return data

def saveConfig(data):
    # print(data)
    config = configparser.ConfigParser()
    
    # 保存内容
    config.add_section('Content Set')
    for i, content in enumerate(data['content'], start=1):
        config.set('Content Set', 'Content' + str(i), content)

    # 保存通讯录模式
    config.add_section('Contracts Set')
    config.set('Contracts Set', 'contractsNumber', str(data['contracts']['contractsNumber']))
    config.set('Contracts Set', 'contractsTime', str(data['contracts']['contractsTime']))
    config.set('Contracts Set', 'timeInterval', str(data['contracts']['timeInterval']))
    config.set('Contracts Set', 'radioContent', str(data['contracts']['radioContent']))
    config.set('Contracts Set', 'radioImage', str(data['contracts']['radioImage']))
    
    # 保存转发群模式
    config.add_section('Group Set')
    config.set('Group Set', 'groupKeyword', str(data['group']['groupKeyword']))
    config.set('Group Set', 'groupNumber', str(data['group']['groupNumber']))
    config.set('Group Set', 'groupMemberNumber', str(data['group']['groupMemberNumber']))
    config.set('Group Set', 'groupTime', str(data['group']['groupTime']))
    config.set('Group Set', 'timeGroupInterval', str(data['group']['timeGroupInterval']))
    config.set('Group Set', 'radioGroupContent', str(data['group']['radioGroupContent']))
    config.set('Group Set', 'radioGroupImage', str(data['group']['radioGroupImage']))
    config.set('Group Set', 'radioGroupLastMessage', str(data['group']['radioGroupLastMessage']))



    # 保存转发好友模式
    config.add_section('Friend Set')
    config.set('Friend Set', 'friendKeyword', str(data['friend']['friendKeyword']))
    config.set('Friend Set', 'friendNumber', str(data['friend']['friendNumber']))
    config.set('Friend Set', 'friendTime', str(data['friend']['friendTime']))
    config.set('Friend Set', 'timeFriendInterval', str(data['friend']['timeFriendInterval']))
    config.set('Friend Set', 'radioFriendContent', str(data['friend']['radioFriendContent']))
    config.set('Friend Set', 'radioFriendImage', str(data['friend']['radioFriendImage']))
    config.set('Friend Set', 'radioFriendLastMessage', str(data['friend']['radioFriendLastMessage']))
    
    # 保存朋友圈
    config.add_section('Moment Set')
    config.set('Moment Set', 'checkboxMoment', str(data['moment']['checkboxMoment']))
    config.set('Moment Set', 'momentNumber', str(data['moment']['momentNumber']))
    config.set('Moment Set', 'timeMomentInterval', str(data['moment']['timeMomentInterval']))
    config.set('Moment Set', 'radioMomentContent', str(data['moment']['radioMomentContent']))
    config.set('Moment Set', 'radioMomentImage', str(data['moment']['radioMomentImage']))
    config.set('Moment Set', 'radioMomentLastMessage', str(data['moment']['radioMomentLastMessage']))
    
    # 保存配置文件
    file = r"C:\\Users\\" + getpass.getuser() + r"\\AppData\\Roaming\\weixin_config.ini"
    with open(file, 'w', encoding='utf-8') as configfile:
        config.write(configfile)


